package com.yuncheng.spcyApi.flowController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.flow.DzdaJysqFlowConstant;
import com.yuncheng.spcyApi.constant.flow.YpscFlowConstant;
import com.yuncheng.spcyApi.entity.SpcyDaSqlc;
import com.yuncheng.spcyApi.entity.SpcySqsxJob;
import com.yuncheng.spcyApi.flowService.DzdaJysqFlowService;
import com.yuncheng.spcyApi.mapper.DaFlowMapper;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.da.DaFlowListVo;
import com.yuncheng.spcyApi.vo.da.DaFlowVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

//@Api(tags = "审评查验-档案借阅申请流程接口")
//@RestController
@RequestMapping("/api/spzx-spcy/flow-dzdajysqlc")
public class DzdaJysqFlowController {
    private static final Logger log = LoggerFactory.getLogger(DzdaJysqFlowController.class);

    @Resource
    private DzdaJysqFlowConstant flowConstant;

    @Resource
    private DaFlowMapper flowMapper;

    @Resource
    private DzdaJysqFlowService flowService;

    @Log(title = "发起借阅申请-流程保存")
    @ApiOperation(value = "发起借阅申请-流程保存")
    @PostMapping(value = "initJysqFlow")
    public HttpResult initJysqFlow(@RequestBody SpcyDaSqlc sqlc){

        Result result = flowService.initJysqFlow(sqlc,false);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
            HttpResult httpResult = new HttpResult(true);
            httpResult.setMessage(result.getMessage());
            httpResult.setResult(result.getResult());
            return httpResult;
        }

    }

    @Log(title = "发去借阅并提交申请-流程提交")
    @ApiOperation(value = "发去借阅并提交申请-流程提交")
    @PostMapping(value = "initSubmitJysqFlow")
    public HttpResult initSubmitJysqFlow(@RequestBody SpcyDaSqlc sqlc){
        Result result = flowService.initJysqFlow(sqlc,true);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
            HttpResult httpResult = new HttpResult(true);
            httpResult.setMessage(result.getMessage());
            httpResult.setResult(result.getResult());
            return httpResult;
        }
    }

    @Log(title = "取消借阅申请流程")
    @ApiOperation(value = "取消借阅申请流程")
    @ApiImplicitParam(name = "jobids",value = "流程id,多个英文逗号分割")
    @PostMapping(value = "cancelJysqFlow")
    public HttpResult cancelJysqFlow(@RequestBody Map<String,String> map){
        String jobids = map.get("jobids");

        return flowService.cancelJysqFlow(jobids);
    }

    @Log(title = "撤回流程")
    @ApiOperation(value = "撤回流程")
    @GetMapping(value = "returnFlow")
    public HttpResult<?> returnFlow(@RequestParam("jobid")String jobid,
                                        @RequestParam(value = "opinion",defaultValue = "")String opinion){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("参数丢失，请联系管理员");
        }
        Result result = flowService.returnFlow(jobid,opinion);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("撤回成功");
    }

    @Log(title = "终止流程")
    @ApiOperation(value = "终止流程")
    @GetMapping(value = "endFlowJob")
    public HttpResult<?> endFlowJob(@RequestParam("jobid") String jobid){

        Result result = flowService.endFlowJob(jobid);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("终止成功");
    }

    @Log(title = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")
    public HttpResult<?> flowJkPage(
            DaFlowListVo flowVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<DaFlowListVo> page = new Page<>(pageNo, pageSize);
        flowVo.setFlcmc(flowConstant.getFlowName());
        IPage<DaFlowListVo> list = flowMapper.dzdajysqFlowJkPage(page,onlyCurrentUser,user,flowVo);
        return HttpResult.ok(list);
    }

    @Log(title = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")
    public HttpResult<?> flowJobPage(
            DaFlowListVo flowVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<DaFlowListVo> page = new Page<>(pageNo, pageSize);
        flowVo.setFlcmc(flowConstant.getFlowName());
        IPage<DaFlowListVo> list = flowMapper.dzdajysqFlowJkPage(page,onlyCurrentUser,user,flowVo);
        return HttpResult.ok(list);
    }


    @Log(title = "流程保存接口")
    @ApiOperation(value = "流程保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody DaFlowVo flowVo) {
        String submitType = YpscFlowConstant.SUBMIT_TYPE_SAVE; // 保存
        String jobId = flowVo.getJobid(); // 当前流程任务id
        String sqid = flowVo.getSqid(); // 当前申请id
        String opinion = flowVo.getOpinion(); // 办理意见

        /*if (StringUtils.isBlank(sqid)){
            return HttpResult.error("申请参数丢失，请联系管理员");
        }*/
        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }

        // 流程处理
        Result httpResult = flowService.handleFlow(flowVo, null, jobId, sqid, submitType, opinion);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }

        Map map = new HashMap();
        HttpResult<Object> res = new HttpResult<>(true);
        res.setMessage("保存成功");
        res.setResult(map);
        return res;
    }

    @Log(title = "流程提交接口")
    @ApiOperation(value = "流程提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody DaFlowVo flowVo) {
        String submitType = YpscFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交

        String jobId = flowVo.getJobid();
        String sqid = flowVo.getSqid();
        String opinion = flowVo.getOpinion();
        /*if (StringUtils.isBlank(sqid)){
            return HttpResult.error("档案参数丢失，请联系管理员");
        }
*/
        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }

        SpcySqsxJob nowJob = null;

        Result httpResult1 = flowService.handleFlow(flowVo, nowJob, jobId, sqid, submitType, opinion);
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }

        return HttpResult.ok("提交成功");
    }

    @Log(title = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(value = "jobid",name = "流程任务id",required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam("jobid")String jobid){
        if (StringUtils.isBlank(jobid)){
            return HttpResult.error("流程参数不能为空");
        }
        Map map = flowService.getJobConcernByJobId(jobid,null);
        return HttpResult.ok(map);
    }

    @Log(title = "查询当前流程任务详情-通过当前申请id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前申请id")
    @ApiImplicitParam(value = "sqid",name = "申请id",required = true)
    @GetMapping(value = "queryJobBySqId")
    public HttpResult<?> queryJobBySqId(@RequestParam(value = "sqid",defaultValue = "")String sqid){
        if (StringUtils.isBlank(sqid)){
            return HttpResult.error("申请参数不能为空");
        }
        Map map = flowService.getJobConcernByJobId(null,sqid);
        return HttpResult.ok(map);
    }

    @Log(title = "管理员-借出纸质档案-借阅档案")
    @ApiOperation(value = "管理员-借出纸质档案-借阅档案")
    @GetMapping(value = "jcjyda")
    public HttpResult jcjyda(@RequestParam(value = "sqid",defaultValue = "") String sqid){
        return flowService.jcjyda(sqid);
    }

    @Log(title = "申请人-归还纸质档案-借阅档案")
    @ApiOperation(value = "申请人-归还纸质档案-借阅档案")
    @GetMapping(value = "ghDzJysq")
    public HttpResult ghDzJysq(@RequestParam(value = "sqid",defaultValue = "") String sqid){
        return flowService.ghDzJysq(sqid,false);
    }

    @Log(title = "管理员-归还纸质档案-借阅档案")
    @ApiOperation(value = "管理员-归还纸质档案-借阅档案")
    @GetMapping(value = "ghDzJysqWithGly")
    public HttpResult ghDzJysqWithGly(@RequestParam(value = "sqid",defaultValue = "") String sqid){
        return flowService.ghDzJysq(sqid,true);
    }

    @Log(title = "管理员-确认归还-借阅档案")
    @ApiOperation(value = "管理员-确认归还-借阅档案")
    @GetMapping(value = "qrghJyda")
    public HttpResult qrghJyda(@RequestParam(value = "sqid",defaultValue = "") String sqid){
        return flowService.qrghJyda(sqid);
    }

}
